import { combineResolvers } from 'graphql-resolvers'
import { pageModel } from '../../lib/helper'
import auth from '../../middlewares/auth'
import dayjs from 'dayjs'
module.exports = {
  Query: {
    getDocumentById: async (root, { id }, { models: { Document } }) => {
      const result = await Document.findById(id)
      return result
    },
    documents: combineResolvers(
      auth,
      async (
        root,
        { pageIndex, pageSize },
        { services: { $document }, user }
      ) => {
        const result = await $document.findAllUserDocuments(
          user.id,
          pageIndex,
          pageSize
        )
        let t = pageModel(result, pageIndex, pageSize)
        return t
      }
    ),
    documentUnpublishes: combineResolvers(
      auth,
      async (
        root,
        data,
        { models: { Document }, user }
      ) => {
        let documents = await Document.findAll({
          where: {
            userId: user.id,
            isPublished: 0,
            documentId: null
          },
          order: [['id', 'desc']]
        })
        return documents
      }
    ),
    findDocumentsByTagId: async (
      root,
      { tagId, cursor: { limit, before, after } },
      { services: { $document } }
    ) => {
      const result = await $document.findDocumentsByTagId({
        tagId,
        cursor: {
          limit,
          before,
          after
        }
      })
      return result
    }
  },
  Mutation: {
    createDocument: combineResolvers(
      auth,
      async (root, { input }, { services: { $document }, user }) => {
        let userId = user.id
        return await $document.create(userId, input)
      }
    ),
    documentPublish: combineResolvers(
      auth,
      async (root, { documentId, groupIds, endAt = '', prePublishAt = '' }, { services: { $document }, user }) => {
        let userId = user.id
        return await $document.publish({userId, documentId, groupIds, endAt, prePublishAt})
      }
    ),
    homeworkPublish: combineResolvers(
      auth,
      async (root, {
        title,
        groupIds,
        prePublishAt = '',
        endAt = '',
        textbookId,
        courseId,
        exercises,
        descriptionBlocks = []
      }, { services: { $task }, user }) => {
        let userId = user.id
        return await $task.publishHomework({
          userId,
          title,
          groupIds,
          prePublishAt,
          endAt,
          textbookId,
          courseId,
          exercises,
          descriptionBlocks
        })
      }
    ),
    updateDocument: combineResolvers(
      auth,
      async (root, { docId, input }, { services: { $document }, user }) => {
        let userId = user.id
        console.info('update document:', docId, input)
        return await $document.update(docId, userId, input)
      }
    )
  },
  Document: {
    user: ({ userId }, arg, { dataloaders: { userLoader } }) => {
      return userLoader.load(userId)
    },
    answerers: (
      { id },
      arg,
      { dataloaders: { userLoaderByDocumentAnswer } }
    ) => {
      return userLoaderByDocumentAnswer.load(id)
    },
    tasks: ({ id }, arg, { dataloaders: { taskLoaderByDocument } }) => {
      return taskLoaderByDocument.load(id)
    },
    unreadCount: (
      { id },
      data,
      { dataloaders: { unreadCountLoaderByDocument } }
    ) => {
      return unreadCountLoaderByDocument.load(id)
    },
    isExpired: ({ endAt }, arg, { dataloaders: { taskLoaderByDocument } }) => {
      if (!endAt) return false
      let flag = dayjs().isAfter(dayjs(endAt))
      return flag
    }
  }
}
